import sys
import pygame

from game.event.eventManager import EventId, Eventbus
from game.container import Variables,CONSTANT, Direction, draw, back, save, load
from game.world.world import World
from game.ui.menus import Menus
from game.sprites.sprites import SpritesContainer

def EventHandler(eventId: EventId):
    """ Decorator to register a function as an event handler """

    def inner(func):
        Eventbus.register(eventId, func)  # type: ignore

        def _inner(*args, **kwargs) -> None:
            func(*args, **kwargs)

        return _inner

    return inner


@EventHandler(EventId.toggle_game_pause)
def toggle_game_pause():
    Variables.is_game_paused = not Variables.is_game_paused


@EventHandler(EventId.game_quit)
def game_quit():
    pygame.quit()
    sys.exit()
    
@EventHandler(EventId.game_start)
def game_start():
    World()
    Variables.world = World.getWorld() # type: ignore
    Variables.is_game_started = True
    Variables.current_ui.jumpto(Menus.game_menu) # type: ignore



@EventHandler(EventId.mouse_button_down)
def mouse_button_down(pos, button):
    match button:

        case 1:  # Left Click
            if (not Variables.is_game_started) or (Variables.is_game_paused):
                Variables.current_ui.on_click(pos)  # type: ignore
                print(pos)
            else:
                World.getWorld().getPlayer().setDestination(pos)

        case 2:  # Middle Click
            pass

        case 3:  # Right Click
            if Variables.is_game_paused:
                pass
            else:
                # World.getWorld().getPlayer().interactWithEntity(SpritesContainer.getMobs()[0])
                World.getWorld().getPlayer().interactWithNearestEntity()

        case 4:  # Scroll Up
            pass

        case 5:  # Scroll Down
            pass


@EventHandler(EventId.mouse_button_up)
def mouse_button_up(pos, button):
    match button:

        case 1:  # Left Click
            if Variables.in_editor_mode:
                draw(Variables.editor_pos1, Variables.world.getPlayer().coordinate)
                Variables.in_editor_mode = False
                Variables.editor_pos1 = None
            else:
                Variables.in_editor_mode = True
                Variables.editor_pos1 = Variables.world.getPlayer().coordinate

        case 2:  # Middle Click
            back()

        case 3:  # Right Click
            Variables.in_editor_mode = False
            Variables.editor_pos1 = None

        case 4:  # Scroll Up
            print(Variables.world.getPlayer().coordinate)

        case 5:  # Scroll Down
            pass


@EventHandler(EventId.key_down)
def key_down(key):
    player = World.getWorld().getPlayer()
    
    match key:

        case 8:  # Backspace
            save()

        case 9:  # tab
            pass

        case 13:  # Enter
            load()

        case 27:  # Escape
            # from game.ui.menus import Menus
            # from game.sound import GameMusicManager

            # if Variables.is_game_paused:
            #     if len(Variables.current_ui.component_list) and Variables.current_ui.is_components_visible:
            #         Variables.current_ui.hide_components()
            #     elif Variables.current_ui == Menus.pause_menu:  # type: ignore
            #         GameMusicManager.unpause()
            #         Eventbus.triggerEvent(EventId.toggle_game_pause)
            #         Variables.current_ui.jumpto(Menus.game_menu)

            #     elif Variables.last_ui is not None:
            #         Variables.current_ui.jumpto(Variables.last_ui)
            # else:
            #     if Variables.current_ui == Menus.game_menu:  # type: ignore
            #         GameMusicManager.pause()
            #         Eventbus.triggerEvent(EventId.toggle_game_pause)
            #         Variables.current_ui.jumpto(Menus.pause_menu, undraw_bg=False)  # type: ignore
            #         Menus.pause_menu.component_list[0].undraw()
            pass

        case 32:  # Space
            from game.ui.menus import Menus
            if Variables.in_dialog:
                Menus.game_menu.component_list[0].next_sentence()

        case 1073742049:  # lshift
            pass

        case 1073742048:  # ctrl
            pass

        case 1073741882:#F1
            pass

        case 1073741883:#F2
            pass

        case 1073741884:#F3
            pass

        case 1073741885:#F4
            pass

        case 1073741886:#F5
            pass

        case 1073741887:#F6
            pass

        case 1073741888:#F7
            pass

        case 1073741889:#F8
            pass

        case 1073741890:#F9
            pass

        case 1073741891:#F10
            pass

        case 1073741892:#F11
            pygame.display.toggle_fullscreen()
            # print(CONSTANT.APP.RESOLUTION)

        case 1073741893:#F12
            pass

        case 119:  # w
            player.direction = Direction.add(player.direction, Direction.N)

        case 97:  # a
            player.direction = Direction.add(player.direction, Direction.W)

        case 115:  # s
            player.direction = Direction.add(player.direction, Direction.S)

        case 100:  # d
            player.direction = Direction.add(player.direction, Direction.E)

        case 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57:  # Digit 0~9
            from game.ui.menus import Menus
            digit = key - 48
            # Menus.game_menu.show_switch()




@EventHandler(EventId.key_up)
def key_up(key):
    player = World.getWorld().getPlayer()
    
    match key:

        case 8:  # Backspace
            pass

        case 9:  # tab
            pass

        case 13:  # Enter
            pass

        case 27:  # Escape
            pass

        case 32:  # Space
            pass

        case 1073742049:  # lshift
            pass

        case 1073742048:  # ctrl
            pass

        case 119:  # w
            player.direction = Direction.sub(player.direction, Direction.N)

        case 97:  # a
            player.direction = Direction.sub(player.direction, Direction.W)

        case 115:  # s
            player.direction = Direction.sub(player.direction, Direction.S)

        case 100:  # d
            player.direction = Direction.sub(player.direction, Direction.E)

        case 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57:  # Digit 0~9
            from game.ui.menus import Menus
            digit = key - 48
            # Menus.game_menu.switch_to(digit)
